﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Collections;
using System.Timers;
using System.Drawing;

using Air_Trafic_Control.Domain.Entity;
using Air_Trafic_Control.Domain.Common;
using Air_Trafic_Control.Domain.Util;

namespace Air_Trafic_Control.Domain.Control
{
    public class RADAR
    {
        private static Logger m_log = new Logger(typeof(RADAR));
        private Point m_RadarLocation;
        private int m_radarDiameter = 0;
        private int m_airportDiameter = 0;
        private int m_maxFlyObjs = 0;
        private int m_flyObjAdditionRate = 0;
        private EAT m_eat;
        private Game m_CurrentGame;

#region Constructor

        /// <summary>
        /// Default constructor for RADAR.
        /// </summary>
        /// <param name="aircraftAdditionRate">The rate in milliseconds at which to add new aircrafts.</param>
        /// <param name="airportDiameter">The diameter of the airport in the center of the radar screen. In pixels.</param>
        /// <param name="maxAircrafts">The maximum number of aircrafts to display at one time.</param>
        /// <param name="radarDiameter">The diameter of the radar. In pixels.</param>
        public RADAR(int maxAircrafts, int aircraftAdditionRate, int radarDiameter, int airportDiameter, Game currentGame)
        {
            this.m_eat = EAT.GetEAT();
            this.m_radarDiameter = radarDiameter;
            this.m_airportDiameter = airportDiameter;
            this.m_RadarLocation = new Point(radarDiameter / 2, radarDiameter / 2);
            this.m_maxFlyObjs = maxAircrafts;
            this.m_flyObjAdditionRate = aircraftAdditionRate;
            this.m_CurrentGame = currentGame;
        }
 
#endregion

#region Properties
        /// <summary>
        /// Accessor and Mutator for the diameter of the radar.
        /// </summary>
        public int RadarDiameter
        {
            get
            {
                return this.m_radarDiameter;
            }
            set
            {
                this.m_radarDiameter = value;
            }
        }

        /// <summary>
        /// Accessor and mutator for the airport diameter.
        /// </summary>
        public int AirportDiameter
        {
            get
            {
                return this.m_airportDiameter;
            }
            set
            {
                this.m_airportDiameter = value;
            }
        }

        /// <summary>
        /// Accessor and Mutator for the current number of aircraft allowed to be displayed at one time.
        /// </summary>
        public int MaxFlyObjs
        {
            get
            {
                return this.m_maxFlyObjs;
            }
            set
            {
                this.m_maxFlyObjs = value;
            }
        }

        /// <summary>
        /// Accessor and Mutator for the current rate at which new aircraft are added to the screen. In milliseconds.
        /// </summary>
        public int FlyObjAdditionRate
        {
            get
            {
                return this.m_flyObjAdditionRate;
            }
            set
            {
                this.m_flyObjAdditionRate = value;
            }
        }

        public Point RadarLocation
        {
            get { return m_RadarLocation; }
        }

#endregion

#region Public Methods
        /// <summary>
        /// Upon every tick of EAT timer, this function updates the inrange flying obj list 
        /// </summary>
        public ArrayList DetectAircrafts()
        {
            ArrayList InRangeAircrafts = new ArrayList();
            lock (m_CurrentGame.FlyingObjs)
            {
                if (this.m_CurrentGame.FlyingObjs != null)
                {
                    foreach (FlyObject flyObj in this.m_CurrentGame.FlyingObjs)
                    {
                        double XSqrDistToCenter = Math.Pow((flyObj.OwnPosition.X - Constants.SYS_RADAR_CENTER), 2);
                        double YSqrDistToCenter = Math.Pow((flyObj.OwnPosition.Y - Constants.SYS_RADAR_CENTER), 2);
                        if (Math.Sqrt(XSqrDistToCenter + YSqrDistToCenter) <= Constants.SYS_RADAR_RADIUS
                            && Math.Sqrt(XSqrDistToCenter + YSqrDistToCenter) >= 15)
                        {
                            InRangeAircrafts.Add(flyObj);
                        }
                    }
                }
            }
            this.m_CurrentGame.CleanupFlyObjList();
            return InRangeAircrafts;
        }

#endregion
    }
}
